//
//  WSUserCenterController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/4.
//

#import "WSUserCenterController.h"
#import "WSUserCenterCell.h"
#import "WSMineUserCell.h"
#import "AppDelegate.h"
#import "CTMediator+WSUserCenterAction.h"

@interface WSUserCenterController () <WSMineUserCellDelegate, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;

@end

@implementation WSUserCenterController

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.listView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

///觀看歷史
- (void)historyAction {
    
}

///我的訂閱
- (void)myFavoriteAction {

}

///我的點讚
- (void)myLikeAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Like];
    [self.navigationController pushViewController:target animated:YES];
}

///幫助反饋
- (void)feedbackAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Feedback];
    [self.navigationController pushViewController:target animated:YES];
}

///我的訂單
- (void)orderAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_CoinRecords];
    [self.navigationController pushViewController:target animated:YES];
}

///設置
- (void)settingAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Setting];
    [self.navigationController pushViewController:target animated:YES];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell;
    if (indexPath.row == 0) {
        WSMineUserCell * userCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSMineUserCell" forIndexPath:indexPath];
        userCell.delegate = self;
        [userCell setDataWithObject:appUserCenter];
        cell = userCell;
    }
    else {
        WSUserCenterCell * userCenterCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSUserCenterCell" forIndexPath:indexPath];
        [userCenterCell setDataWithObject:self.dataArray[indexPath.row]];
        cell = userCenterCell;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth - 12 - 12;
    if (indexPath.row == 0) {
        size.height = 190 + 25;
    }
    else {
        size.height = 57;
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = self.dataArray[indexPath.row];
    SEL sel = NSSelectorFromString(dic[@"sel"]);
    if ([self respondsToSelector:sel]) {
        [self performSelector:sel withObject:nil];
    }
}

#pragma mark GRMineUserCellDelegate
- (void)mineUserCellUserAction {
    if (appUserCenter.isLogin) {
        UIViewController *userInfoVC = [[CTMediator sharedInstance]ct_showcharonTV_UserInfoWithUser:appUserCenter];
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }
    else {
        [WSTools pushLoginController];
//        [((AppDelegate*)([UIApplication sharedApplication].delegate)) initLoginPage];
    }
}

- (void)mineUserCellCoinAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:1];
    [self.navigationController pushViewController:target animated:YES];
}

- (void)mineUserCellVipAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:0];
    [self.navigationController pushViewController:target animated:YES];
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@{@"title":@"",
                         @"image":@"",
                         @"sel":@""},
                       @{@"title":@"我的點讚",
                         @"image":@"icon_wddy",
                         @"sel":@"myLikeAction"},
                       @{@"title":@"幫助反饋",
                         @"image":@"icon_bzfk",
                         @"sel":@"feedbackAction"},
                       @{@"title":@"我的訂單",
                         @"image":@"icon_wddd",
                         @"sel":@"orderAction"},
                       @{@"title":@"設置",
                         @"image":@"icon_sz",
                         @"sel":@"settingAction"},];
    }
    return _dataArray;
}
@end
